// pages/recruit/personal/resume/addeducation/addeducation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qualifications: ["初中", "高中/职高", "专科", "本科", "硕士", "博士"],
    qualkey: 3,
    ishowtext: "show",
    textlimit: 0,
    education: {
      school:"",
      subject: "",
      education:"请选择",
      admissionDate: "请选择",
      graduationDate: "请选择",
      experience: ""
    },
    state: "new",//默认新建，进来时带有状态时则变为修改。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('crossdata', function (res) {
      if (res.data.experience.length != 0){
        that.setData({
          ishowtext: "",
          textlimit: res.data.experience.length
        })
      }
      that.setData({
        education: res.data,
        state: "edit"
      })
    })
  },
  schoolinput: function (e) {
    const that = this;
    that.data.education.school = e.detail.value;
    that.setData({
      education: that.data.education,
    })
  },
  majorinput: function (e) {
    const that = this;
    that.data.education.subject = e.detail.value;
    that.setData({
      education: that.data.education,
    })
  },
  qualifications: function(e){
    const that = this;
    that.data.education.education = that.data.qualifications[parseInt(e.detail.value)];
    that.setData({
      education: that.data.education,
      qualkey: parseInt(e.detail.value)
    })
  },
  begintime:function(e){
    const that = this;
    that.data.education.admissionDate = e.detail.value;
    that.setData({
      education: that.data.education,
    })
  },
  endtime: function (e) {
    const that = this;
    that.data.education.graduationDate = e.detail.value;
    that.setData({
      education: that.data.education,
    })
  },
  textarea: function(e){
    const that = this;
    if (e.detail.value.length>0){
      that.setData({
        ishowtext: ""
      })
    }else{
      that.setData({
        ishowtext: "show"
      })
    }
    that.data.education.experience = e.detail.value;
    that.setData({
      education: that.data.education,
      textlimit: e.detail.value.length
    })
  },
  back: function () {
    wx.navigateBack({})
  },
  commit: function () {
    const that = this;
    var box = that.data.education;
    var title;
    if (box.school == "") {
      title = "学校名称不能为空"
    } else if (box.education == "请选择") {
      title = "请选择你的最高学历"
    } else if (box.admissionDate == "请选择") {
      title = "请选择你的入学时间"
    } else if (box.graduationDate == "请选择") {
      title = "请选择你的毕业时间"
    } else {
      if (that.data.state == "new") {
        //新增加一个resumeId11
        box.resumeId = getApp().globalData.resumeId;
        getApp().globalData.post("/api/resume/education", box).then(res => {
          wx.navigateBack();
        })
      } else {
        console.log("commit")
        getApp().globalData.post("/api/resume/education", box).then(res => {
          wx.navigateBack();
        })
      }
      return;
    }
    wx.showToast({
      title: title,
      icon: "none",
      duration: 1000
    })
  }
})